#!/bin/bash


function npm-source {
    if [ x$1 == xreset ]; then
        yarn config set registry https://registry.yarnpkg.com
        npm config set registry https://registry.npmjs.org/
    elif [ x$1 == xtb ]; then
        yarn config set registry https://registry.npm.taobao.org
        npm config set registry https://registry.npm.taobao.org
    elif [ x$1 == x31 ]; then
        yarn config set registry http://192.168.1.31:4873
        npm config set registry http://192.168.1.31:4873
    elif [ x$1 == xrm ]; then
        yarn config delete registry 
        npm config delete registry 
    elif [ x$1 == x ]; then
        yarn config get registry 
        npm config get registry 
    fi
}

function _nr_completion() {

    local words cword
    _get_comp_words_by_ref -n = -n @ -n : -w words -i cword

    if [ $cword -gt 1 ]; then return 0; fi

    local point=8 #'npm run '的长度
    local w1=${COMP_WORDS[1]:-''}
    local wlen=${#w1}
    point=$((point+wlen))
    #echo !!!w1:$w1, point:$point
    local si="$IFS"
    #set -x
    IFS=$'\n' COMPREPLY=($(COMP_CWORD=2 COMP_LINE="npm run $w1" COMP_POINT=$point npm completion -- npm run "$w1" 2>/dev/null)) || return $?
    #set +x
    IFS="$si"
    #echo !!!cword:$COMP_CWORD, words:$w1, point:$point, reply:${COMPREPLY[@]},----
    #COMPREPLY=(a b c)
    __ltrim_colon_completions "${words[cword]}"
}
complete -o default -F _nr_completion nr

export PATH=~/.yarn/bin:$PATH
command -v fnm > /dev/null  &&  eval "`fnm env`" #|| echo 'no fnm'

alias nr='yarn run'
alias npm-taobao="npm config set registry https://registry.npm.taobao.org"
alias tnpm="npm --registry https://mirrors.tencent.com/npm/"

if [ "$OS" == "Darwin" ] ;then
alias install-fnm="brew install Schniz/tap/fnm"

else
alias install-fnm="curl -fsSL https://github.com/Schniz/fnm/raw/master/.ci/install.sh | bash --skip-shell"
fi
